#ifndef PLAYERMODEL_H
#define PLAYERMODEL_H

#include <QObject>
#include <QString>
#include <QSettings>
#include <QVariantList>
#include <QSet>
#include "PlayerProfile.h"

class PlayerModel : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QString avatar READ avatar WRITE setAvatar NOTIFY avatarChanged)
    Q_PROPERTY(int totalPoints READ totalPoints WRITE setTotalPoints NOTIFY totalPointsChanged)
    Q_PROPERTY(int totalStars READ totalStars WRITE setTotalStars NOTIFY totalStarsChanged)
    
    
    // 云同步配置属性
    Q_PROPERTY(QString giteeOwner READ giteeOwner WRITE setGiteeOwner NOTIFY giteeOwnerChanged)
    Q_PROPERTY(QString giteeRepo READ giteeRepo WRITE setGiteeRepo NOTIFY giteeRepoChanged)
    Q_PROPERTY(QString giteeToken READ giteeToken WRITE setGiteeToken NOTIFY giteeTokenChanged)
    Q_PROPERTY(bool cloudSyncEnabled READ cloudSyncEnabled NOTIFY cloudSyncEnabledChanged)
    Q_PROPERTY(QVariantList medals READ medals NOTIFY medalsChanged)

public:
    explicit PlayerModel(QObject *parent = nullptr);

    // Getters
    QString name() const { return m_profile.name; }
    QString avatar() const { return m_profile.avatar; }
    int totalPoints() const { return m_profile.totalPoints; }
    int totalStars() const { return m_profile.totalStars; }
    
    
    // 云同步配置 Getters
    QString giteeOwner() const { return m_giteeOwner; }
    QString giteeRepo() const { return m_giteeRepo; }
    QString giteeToken() const { return m_giteeToken; }
    bool cloudSyncEnabled() const;
    QVariantList medals() const { return m_medals; }

    // Setters
    void setName(const QString& name);
    void setAvatar(const QString& avatar);
    void setTotalPoints(int points);
    void setTotalStars(int stars);
    
    
    // 云同步配置 Setters
    void setGiteeOwner(const QString& owner);
    void setGiteeRepo(const QString& repo);
    void setGiteeToken(const QString& token);

    // QML调用的方法
    Q_INVOKABLE void saveProfile();
    Q_INVOKABLE void loadProfile();
    Q_INVOKABLE void addPoints(int points);
    Q_INVOKABLE void addStars(int stars);
    
    
    // 头像资源管理
    Q_INVOKABLE bool checkForNewAvatars();  // 检查是否有新的头像文件
    Q_INVOKABLE bool updateAvatarResources();  // 更新qml.qrc和QML文件
    Q_INVOKABLE QStringList getNewAvatarList();  // 获取新头像列表
    Q_INVOKABLE bool hasMedal(const QString& treeId, int skillId) const;

signals:
    void nameChanged();
    void avatarChanged();
    void totalPointsChanged();
    void totalStarsChanged();
    
    
    // 云同步配置信号
    void giteeOwnerChanged();
    void giteeRepoChanged();
    void giteeTokenChanged();
    void cloudSyncEnabledChanged();
    void medalsChanged();
    
    // 头像更新信号
    void avatarUpdateSuccess(QString message);
    void avatarUpdateFailed(QString message);

public slots:
    void addMedal(const QString& treeId, int skillId, const QString& name, const QString& icon);
    void removeMedal(const QString& treeId, int skillId);
    void updateMedalInfo(const QString& treeId, int skillId, const QString& newName, const QString& icon);

private:
    PlayerProfile m_profile;
    QSettings m_settings;
    
    // 云同步配置（单独的配置文件）
    QSettings* m_cloudSyncSettings;
    QString m_giteeOwner;
    QString m_giteeRepo;
    QString m_giteeToken;
    
    // 头像管理
    QStringList m_newAvatars;  // 缓存新发现的头像列表
    
    // 勋章管理
    QVariantList m_medals;
    QSet<QString> m_medalIds;

    void initializeDefaultProfile();
    void loadCloudSyncConfig();
    void saveCloudSyncConfig();
    
    // 头像资源管理辅助方法
    QString findSourceDirectory() const;  // 查找源码目录
    QString makeMedalKey(const QString& treeId, int skillId) const;
};

#endif // PLAYERMODEL_H

